<?php
		  require_once "trackingModify.php";

		$isSuccess = 0 ;
			
			
			//****************************************
			// lay ra cac thong tin co ban tu` href
			//****************************************
			$id_type = $_POST['lTypes'];
			$id_item = $cFilter -> filter($_POST['tf_idItem']);
			$name = $cFilter -> filter($_POST['tf_name']);
			$price = $cFilter -> filter($_POST['tf_price']);
			$status_price = $_POST['lPrice'];
			$status_sell = -1;
			
			$description = $_POST['tf_description'];
			$favorite = 0 ; 
			$isNew = mktime(0,0,0,date('m'),date('d'),date('Y'));
			$manufacture = $cFilter -> filter($_POST['tf_manufacture']);
			
			$status = 1; 
			
			$script_performance ="Chua co hien thi cho dong` san pham nay ";
			
			$picture = $_FILES['tf_filePicture'];
			//$thumbnail = $_FILES['tf_fileThumbnail'];
			
			$pictureFilePath = "../resources/".time()."_".basename($picture['name']);
			$thumbnailFilePath = "../resources/".time()."_".basename($picture['name']);//"../resources/".time()."_".basename($thumbnail['name']);
			
			$pictureInfo = getimagesize($picture['tmp_name']);
			list($width, $height) = getimagesize($picture['tmp_name']);
			//$thumbnailInfo = getimagesize($thumbnail['tmp_name']);
			
						
			$blacklist = array('.php', '.phtml');
	
			if($name =="")			
			{
					echo '<script language="javascript">window.location.href="main.php?jmp=items&to=idbadd&name='.$name.'&showType='.$id_type.'&price='.$price.'&status_price='.$status_price.'&manufacture='.$manufacture.'&id_items='.$id_item.'&notification=ITEM_ACTION_ADD_ERROR_NAME"</script>';  
				return ;
			}
			
						
			if(!is_numeric($price) || $price=="")	
			{
				echo '<script language="javascript">window.location.href="main.php?jmp=items&to=idbadd&name='.$name.'&showType='.$id_type.'&price='.$price.'&status_price='.$status_price.'&manufacture='.$manufacture.'&id_items='.$id_item.'&notification=ITEM_ACTION_ADD_ERROR_PRICE"</script>';  
				return ;
			}
			
			if($id_item =="")
			{
				echo '<script language="javascript">window.location.href="main.php?jmp=items&to=idbadd&name='.$name.'&showType='.$id_type.'&price='.$price.'&status_price='.$status_price.'&manufacture='.$manufacture.'&id_items='.$id_item.'&notification=ITEM_ACTION_ADD_ERROR_ITEMCODE"</script>';  
				return ;				
			}
						
			
			
			if($id_type<=0)
			{
				 
				echo '<script language="javascript">window.location.href="main.php?jmp=items&to=idbadd&name='.$name.'&showType='.$id_type.'&price='.$price.'&status_price='.$status_price.'&manufacture='.$manufacture.'&id_items='.$id_item.'&notification=ITEM_ACTION_ADD_ERROR_TYPE"</script>';
				return ;
			}	

			else
			{	
				
				
				/*-------------------------------------------------------
				//
				// Xu ly upload File 
				//
				--------------------------------------------------------*/
				
				if(	
					( $picture['type'] == 'image/gif' || $picture['type'] == 'image/jpeg' || $picture['type'] == 'image/png' ) 
					&& 
					( $pictureInfo['mime'] =='image/gif' || $pictureInfo['mime'] == 'image/jpeg' ||  $pictureInfo['mime'] == 'image/png' ) 
					&& 
					!preg_match('/$blacklist[0]\$/i', $picture['name']) 
					&& 
					!preg_match('/$blacklist[1]\$/i', $picture['name'])				
				   ) 
				{
				
						/*----------------------------------------------------
						
							cho nay xu ly cai thumnail
						
						-----------------------------------------------------*/
						
						/*if(	
							($thumbnail['type'] == 'image/gif' || $thumbnail['type'] == 'image/jpeg' || $thumbnail['type'] == 'image/png')	
							&& 
							($thumbnailInfo['mime'] =='image/gif' || $thumbnailInfo['mime'] == 'image/jpeg' || $thumbnailInfo['mime'] == 'image/png' ) 
							&& 
							!preg_match('/$blacklist[0]\$/i', $thumbnail['name']) && 
							!preg_match('/$blacklist[1]\$/i', $thumbnail['name'])				
							) 
						{
							move_uploaded_file($thumbnail['tmp_name'], $thumbnailFilePath);
						}
						else
						{
							$thumbnailFilePath = $pictureFilePath;
						}*/
								
						move_uploaded_file($picture['tmp_name'], $pictureFilePath);
						
						$thumbnail_type='vertical_type';
						if($width>$height)
							$thumbnail_type='horizontal_type';
							
						$item = new CItems ;
						$isSuccess  = $item ->CItemsAdd($id_type,$name,$price,$status_price,$status_sell,$status,$pictureFilePath,$favorite,$isNew,$description,$manufacture,$thumbnail_type,$script_performance,$id_item);
	
							
				}
				//file upload khong thoa cac dieu kien 
				else
				{
					echo '<script language="javascript">window.location.href="main.php?jmp=items&to=idbadd&name='.$name.'&showType='.$id_type.'&price='.$price.'&status_price='.$status_price.'&manufacture='.$manufacture.'&id_items='.$id_item.'&notification=ITEM_ACTION_ADD_ERROR_PICTURE"</script>';
						 return ;
				}	
				
			}
								
			
			
			if ($isSuccess <= 0)
			{
				echo '<script language="javascript">window.location.href="main.php?jmp=items&to=idbadd&name='.$name.'&showType='.$id_type.'&price='.$price.'&status_price='.$status_price.'&manufacture='.$manufacture.'&id_items='.$id_item.'&notification=XXXX_ACTION_TO_SERVER"</script>';
				 return ;					
			}
			else
			{
				echo '<script language="javascript">window.location.href="main.php?jmp=items&to=idbview&notification=ITEM_ACTION_ADD_SUCCESS"</script>';				
				return ;
			}	
?>